#!/bin/bash
## Copyright (c) 2010 The Hursk Group, LLC. All rights reserved.
## 
## www.hursk.com
## 
## hurskgroup@hursk.com
## 
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE.

##
## This var sets the directory to have its
## files hashed

DIR_TO_HASH="/opt/local/bin/"

##
## Recursive allows for the hashing of files to occur recursively
## from the DIR_TO_SCAN variable 0 = no, 1 = yes

RECURSIVE=0

##
## This is where you put the URL for a server that will
## supply xml results of a hash

NSRL_Server="http://www.hursk.com/filehashdb/test_34bob/hash/"

##
## DON'T TOUCH THE VARIABLES THAT FOLLOW
## DON'T TOUCH THE VARIABLES THAT FOLLOW
## DON'T TOUCH THE VARIABLES THAT FOLLOW
## DON'T TOUCH THE VARIABLES THAT FOLLOW
DTS_START=`date "+%Y_%m_%d_%H%M%S"`
DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
EXE_DIR=./
WORKING_DIR=hash_analysis_$DTS_START
LOG_FILE=$WORKING_DIR/aa_hash_analysis.log
HASH_RESULTS_FILE=$WORKING_DIR/hash_results.txt
CSV_OUTPUT=$WORKING_DIR/workbook_hash_analysis.csv
SERVER_RESPONSE_DIR=$WORKING_DIR/server_response
##


## STEP 1
## Generate working directory
## this is relative to this executables
## location.  Touch log file, and copy
## this executable (for refrence) into it

mkdir $WORKING_DIR
mkdir $SERVER_RESPONSE_DIR
cp run_me.bash $WORKING_DIR
touch $LOG_FILE
touch $CSV_OUTPUT
echo "********************" >> $LOG_FILE
echo "*** STEP 1 START ***" >> $LOG_FILE
echo $DTS_START >> $LOG_FILE

echo "Hash Analysis of: "$DIR_TO_HASH >> $LOG_FILE
echo "... with Recursive set to: "$RECURSIVE >> $LOG_FILE
echo "... has been initiated" >> $LOG_FILE

DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE
echo "**** STEP 1 END ****" >> $LOG_FILE
echo "********************" >> $LOG_FILE

## END STEP 1


## STEP 2
## Execute the hashdeep command
## stderror is writen to log file

echo "********************" >> $LOG_FILE
echo "*** STEP 2 START ***" >> $LOG_FILE
DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE

echo "hashing has begun" >> $LOG_FILE

if [ $RECURSIVE -eq 0 ] # NO RECURSION
	then ./hashdeep -c md5 $DIR_TO_HASH* > $HASH_RESULTS_FILE 2>> $LOG_FILE
	else ./hashdeep -c md5 $DIR_TO_HASH* > $HASH_RESULTS_FILE 2>> $LOG_FILE
fi

echo "hashing has completed" >> $LOG_FILE
DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE

HASHED_FILES_COUNT=`cat $HASH_RESULTS_FILE | grep -v "%%%%" | grep -v "##" | wc -l`
echo "number of files hashed: "$HASHED_FILES_COUNT >> $LOG_FILE

echo "Number of files hashed,"$HASHED_FILES_COUNT >> $CSV_OUTPUT

DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE
echo "**** STEP 2 END ****" >> $LOG_FILE
echo "********************" >> $LOG_FILE

## END STEP 2


## STEP 3
## Call the server and get xml response for 
## information about the file

echo "********************" >> $LOG_FILE
echo "*** STEP 3 START ***" >> $LOG_FILE
DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE

echo "server calls have started" >> $LOG_FILE

for MD5 in `cat $HASH_RESULTS_FILE | grep -v "%%%%" | grep -v "##" | cut -f2 -d,`
do
	wget -q -O $SERVER_RESPONSE_DIR/$MD5 $NSRL_Server$MD5 2>> $LOG_FILE
done

echo "server calls have ended" >> $LOG_FILE

DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE
echo "**** STEP 3 END ****" >> $LOG_FILE
echo "********************" >> $LOG_FILE

## END STEP 3


## STEP 4
## Gather response statistics
## 

echo "********************" >> $LOG_FILE
echo "*** STEP 4 START ***" >> $LOG_FILE
DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE

FILES_IN_DB=`cat $SERVER_RESPONSE_DIR/* | grep -v "Hash value is NOT in the" | grep -v "xml version" | wc -l`
echo "Number of file hashes in db,"$FILES_IN_DB >> $LOG_FILE

echo "Number of file hashes in db,"$FILES_IN_DB >> $CSV_OUTPUT

DTS_CURRENT=`date "+%Y_%m_%d_%H%M%S"`
echo $DTS_CURRENT >> $LOG_FILE
echo "**** STEP 4 END ****" >> $LOG_FILE
echo "********************" >> $LOG_FILE

## END STEP 4


## STEP 3
## 
## stderror is writen to log file

## END STEP 3
